package com.example.douyinproject;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;

import java.util.ArrayList;
import java.util.List;

/*申请隐私权限的过程
 * 1、判断是否给了权限
 * 2、申请权限
 * 3、查看权限的结果
 * */
public class HomeActivity extends AppCompatActivity {
    String []permissions = {Manifest.permission.READ_EXTERNAL_STORAGE}; //将要申请的权限放在数组中
    //2、创建集合，逐个判断哪些权限未授予，将其放到集合中
    List<String>mPermissionList = new ArrayList<>();
    int requestCode = 12138;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //判断手机版本号
        if (Build.VERSION.SDK_INT >= 23) {
            //申请权限
            initPermission();
        } else {
            handler.sendEmptyMessageDelayed(100,2000);
        }
    }

    /*初始化权限的方法：判断是否给了权限，没给就申请*/
    private void initPermission() {
        mPermissionList.clear(); //清空没有通过的权限
        for (int i = 0; i < permissions.length; i++) {
            int res = ActivityCompat.checkSelfPermission(this, permissions[i]); //核对用户是否给权限了
            if (res != PackageManager.PERMISSION_GRANTED) { //如果没有授予权限，就将他们放到集合当中统一管理
                mPermissionList.add(permissions[i]);
            }
        }

        if (mPermissionList.size() > 0) {
            ActivityCompat.requestPermissions(this,permissions,requestCode);
        } else {
            //权限都给了，可以进行跳转了
            handler.sendEmptyMessageDelayed(100,2000);
        }
    }

    /*获取申请权限的结果*/
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean flag = false; //判断权限是否都给了的标志位
        if (requestCode == this.requestCode) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == -1) {
                    flag = true;
                }
            }

            if (flag) { //有权限没有被用户运行
                showSettingDialog();
            } else {
                handler.sendEmptyMessageDelayed(100,2000);
            }
        }
    }

    /*弹出对话框，问是否跳转设置页面*/
    private void showSettingDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("没有提供权限，无法打开页面，是否跳转设置页面打开权限？")
                .setIcon(R.mipmap.icon)
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package", HomeActivity.this.getPackageName(), null);
                        intent.setData(uri);
                        startActivity(intent);
                    }
                });
        builder.create().show();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 100) {
                Intent it = new Intent(HomeActivity.this, MainActivity.class);
                startActivity(it);
                finish();
            }
        }
    };
}